package cn.zktr.Personnel.model.service.zhouyongcong.monit;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zktr.Personnel.model.pojos.PerAudit;
import cn.zktr.Personnel.model.pojos.PerAuditTab;
import cn.zktr.Personnel.model.pojos.PerAuditZd;
import cn.zktr.Personnel.model.service.zhouyongcong.boolService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveAuditService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveService;

@Service
@Transactional(rollbackFor=Exception.class)
public class monitService {
	@Autowired
	private saveService ss;
	@Autowired
	private saveAuditService sas;
	@Autowired
	private boolService bl;

	
	public int save(Object obj_old,Object obj_new,PerAudit pa,int cha,boolean dle) {
		PerAuditTab pat = getPad(obj_new);
		return save_for(obj_old,obj_new,pa,pat,cha,dle);
	}
	public int save(Object obj_new,PerAudit pa,int cha) {
		PerAuditTab pat = getPad(obj_new);
		return save_for(obj_new,pa,pat,cha);
	}
	private int save_for(Object obj_old,Object obj_new,PerAudit pa,PerAuditTab pat,int cha,boolean dle) {
		//一个有梦想的自增id
		int amd = bl.bool_Mdy();
		//表的主键id名
		String minname = "";
		//表的主键id
		Integer minid = null;
		Class<? extends Object> cla = null;
		cla = obj_new.getClass();
		Field[] fields = cla.getDeclaredFields();
		for(Field field : fields) {
			if(field.getAnnotation(Id.class)!=null) {		//主表字段id
				minname = field.getName();
				Object text = getText(obj_old, minname);
				minid = Integer.valueOf(text+"")+cha;
			}
		}
		for(Field field : fields) {
			JoinColumn jc = field.getAnnotation(JoinColumn.class);
			Id id = field.getAnnotation(Id.class);
			OneToMany otm = field.getAnnotation(OneToMany.class);
			if(!field.getName().equals("serialVersionUID")&&id==null&&otm==null&&jc==null){//本数据表
				PerAuditZd paz = getPaz(field.getName(),pat);
				Object old_text = getText(obj_old, field.getName());
				Object new_text = getText(obj_new, field.getName());
				if(dle) {
					if(new_text.equals(old_text)) {
						System.err.println("属性未修改，不上传");
					}else {
						System.err.println(field.getName()+"属性的值为："+new_text);
						ss.save_Mdy(amd++,pat,paz,pa,minname,minid,new_text,old_text);
					}
				}else {
					if(new_text!=null) {
						if(new_text.equals(old_text)) {
							System.err.println("属性未修改，不上传");
						}else {
							System.err.println(field.getName()+"属性的值为："+new_text);
							ss.save_Mdy(amd++,pat,paz,pa,minname,minid,new_text,old_text);
						}
					}
					System.err.println("属性未修改，不上传");
				}
			}else if(jc!=null){								//他表主键id
				PerAuditZd paz = getPaz(jc.name().toLowerCase(),pat);
				Object old_maai = getText(obj_old, field.getName());
				Object new_maai = getText(obj_new, field.getName());
				Object old_text = null;
				Object new_text = null;
				old_text = getText(old_maai, getname(jc.name().toLowerCase()));
				new_text = getText(new_maai, getname(jc.name().toLowerCase()));
				
				if(dle) {
					if(new_text==null||old_text==null) {
						if(new_text==old_text) {
							System.err.println("属性未修改，不上传");
							continue;
						}else {
							ss.save_Mdy(amd++,pat,paz,pa,minname,minid,new_text,old_text);
						}
					}else {
						if(new_text.equals(old_text)) {
							System.err.println("属性未修改，不上传");
							continue;
						}else {
							ss.save_Mdy(amd++,pat,paz,pa,minname,minid,new_text,old_text);
						}
					}
				}else {
					if(new_text!=null) {
						if(new_text.equals(old_text)) {
							System.err.println("属性未修改，不上传");
							continue;
						}else {
							ss.save_Mdy(amd++,pat,paz,pa,minname,minid,new_text,old_text);
						}
					}else {
						System.err.println("属性未修改，不上传");
						continue;
					}
				}
			}
		}
		return minid;
	}
	private int save_for(Object obj_new,PerAudit pa,PerAuditTab pat,int cha) {
		//一个有梦想的自增id
		int amd = bl.bool_Mdy();
		//表的主键id名
		String minname = "";
		//表的主键id
		Integer minid = null;
		
		Class<? extends Object> cla = obj_new.getClass();
		Field[] fields = cla.getDeclaredFields();
		for(Field field : fields) {
			if(field.getAnnotation(Id.class)!=null) {		//主表字段id
				minname = field.getName();
				Object text = getText(obj_new, minname);
				minid = Integer.valueOf(text+"")+cha;
				PerAuditZd paz = getPaz(minname,pat);
				ss.save_Mdy(amd++,pat,paz,pa,minname,minid,minid);
			}
		}
		for(Field field : fields) {
			JoinColumn jc = field.getAnnotation(JoinColumn.class);
			Id id = field.getAnnotation(Id.class);
			OneToMany otm = field.getAnnotation(OneToMany.class);
			if(!field.getName().equals("serialVersionUID")&&id==null&&otm==null&&jc==null){//本数据表
				PerAuditZd paz = getPaz(field.getName(),pat);
				Object text = getText(obj_new, field.getName());
				System.err.println(field.getName()+"属性的值为："+text);
				ss.save_Mdy(amd++,pat,paz,pa,minname,minid,text);
			}else if(jc!=null){								//他表主键id
				PerAuditZd paz = getPaz(jc.name().toLowerCase(),pat);
				Object maai = getText(obj_new, field.getName());
				Object text = null;
				if(maai!=null) {
					text = getText(maai, getname(jc.name().toLowerCase()));
				}
				ss.save_Mdy(amd++,pat,paz,pa,minname,minid,text);
			}
		}
		return minid;
	}
	private String getname(String tabname) {
		if(tabname.indexOf("_")==-1) {
			return tabname;
		}else {
			return tabname.split("_")[1];
		}
	}
	private Object getText(Object obj_new,String textname) {
		try {
			Method mm = obj_new.getClass().getMethod(getMethod(textname));
			System.err.print("查询方法："+getMethod(textname));
			System.err.println("————"+mm.invoke(obj_new));
			return mm.invoke(obj_new);
		} catch (Exception e) {
			System.err.println("报错！"+textname+"属性将为空！");
			return null;
		}
	}
	private String getMethod(String textname) {
		String str = textname.charAt(0)+"";
		textname = "get"+str.toUpperCase()+textname.substring(1);
		return textname;
	}
	private PerAuditTab getPad(Object obj_new) {
		try {
			Class<? extends Object> cla = obj_new.getClass();		//新建实体类的Class类型对象
			Table annotation = cla.getAnnotation(Table.class);		//获取类上的注解
			String tabname = annotation.name();						//获取注解的属性值————获取数据库名字
			System.out.println("以查询到的表:"+tabname);
			PerAuditTab pat = sas.save_Tab(tabname,bl.bool_Tab(tabname));		//入库
			ss.save_Tab(pat);
			return pat;
		} catch (Exception e) {
			System.out.println("添加表失败");
			return null;
		}
	}
	private PerAuditZd getPaz(String ztname,PerAuditTab pat) {
		try {
			PerAuditZd paz = sas.save_Zd(ztname, bl.bool_Zd(ztname, pat), pat);
			ss.save_Zd(paz);
			return paz;
		} catch (Exception e) {
			System.out.println("添加字段失败");
			return null;
		}
	}
}
